package com.mojian.quartz;

import com.mojian.common.RedisConstants;
import com.mojian.entity.SysArticle;
import com.mojian.mapper.SysArticleMapper;
import com.mojian.utils.RedisUtils;
import com.mojian.vo.article.ArticleDetailVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@Slf4j
@Component("task")
@RequiredArgsConstructor
public class TaskQuartz {

    private final RedisUtils redisUtils;

    private final SysArticleMapper sysArticleMapper;

    public void neatMultipleParams(String s, Boolean b, Long l, Double d, Integer i) {
      //  System.out.println(StringUtils.format("执行多参方法： 字符串类型{}，布尔类型{}，长整型{}，浮点型{}，整形{}", s, b, l, d, i));
    }

    public void neatParams(String params) {
        System.out.println("执行有参方法：" + params);
    }

    public void neatNoParams() {
        System.out.println("执行无参方法");
    }

    /**
     * 同步阅读数据
     */
    public void syncViewData(){
        log.info(">>>>>>>>>>>>>>>>>>>>>>>>>开始同步阅读数据");
        Map<Object, Object> map = redisUtils.hGetAll(RedisConstants.ARTICLE_VIEW_COUNT);
        map.forEach((k,v)->{
            Long articleId = Long.parseLong(k.toString());
            Integer viewCount = Integer.parseInt(v.toString());
            SysArticle sysArticle = sysArticleMapper.selectById(articleId);
            sysArticle.setQuantity(sysArticle.getQuantity()+viewCount);
            sysArticleMapper.updateById(sysArticle);
            redisUtils.hDel(RedisConstants.ARTICLE_VIEW_COUNT,k);
        });
        log.info(">>>>>>>>>>>>>>>>>>>>>>同步阅读数据结束");
    }
}
